<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="产线或产线组">
              <j-dict-select-tag placeholder="请选择产线或产线组" v-model="queryParam.line"
                                 dictCode="mes_line_cfg,LINE,LINE" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="4" :sm="4">
            <a-form-item label="工站">
              <op-select placeholder="请选择工站" v-model="queryParam.op" :line="queryParam.line"></op-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="4" :sm="4">
            <a-form-item label="查询维度">
              <j-dict-select-tag placeholder="请选择查询维度" v-model="queryParam.dimension" dictCode="time_dimension"
                                 @change="dimensionChange" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="查询范围">
              <a-date-picker :show-time="{ format: format }" :format=format :valueFormat=format placeholder="Start Time"
                             v-model="queryParam.startTime" />
              <span>~</span>
              <a-date-picker :show-time="{ format: format }" :format=format :valueFormat=format placeholder="End Time"
                             v-model="queryParam.endTime" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="ExportXls(true)">导出追溯报表</a-button>
      <a-button type="primary" icon="download" @click="ExportXls(false)">源数据</a-button>
      <!-- 高级查询区域 -->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
      </a-table>
    </div>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import OpSelect from '@comp/mes/OpSelect.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { getAction } from '@api/manage'

export default {
  name: 'EqpMachinestateValueList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    OpSelect
  },
  data() {
    return {
      description: '设备运行状态管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '产线或产线组',
          align: 'center',
          sorter: true,
          dataIndex: 'line'
        },
        {
          title: '工站',
          align: 'center',
          sorter: true,
          dataIndex: 'description'
        },
        {
          title: '设备编号',
          align: 'center',
          sorter: true,
          dataIndex: 'eqpNo'
        },
        {
          title: '时间',
          align: 'center',
          sorter: true,
          dataIndex: 'date'
        },
        {
          title: '设备状态',
          align: 'center',
          sorter: true,
          dataIndex: 'status_dictText'
        },
        {
          title: '持续时间（min）',
          align: 'center',
          sorter: true,
          dataIndex: 'time'
        },
        {
          title: '占比(%)',
          align: 'center',
          sorter: true,
          dataIndex: 'proportion'
        }
      ],
      url: {
        list: '/eqp/eqpMachinestateValue/eqpMachinestateTrace',
        exportXlsUrl: '/eqp/eqpMachinestateValue/exportXls'
      },
      oneLoad: 1,
      format: 'YYYY-MM-DD',
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    ExportXls(isYuan) {
      if (isYuan) {
        this.url.exportXlsUrl = '/eqp/eqpMachinestateValue/exportTraceXls'
        this.handleExportXls('设备状态追溯')
      } else {
        this.url.exportXlsUrl = '/eqp/eqpMachinestateValue/exportXls'
        this.handleExportXls('设备状态源数据')
      }
    },
    //根据查询维度改变日期范围
    dimensionChange(val) {
      if (val === 'hour') {
        this.format = 'YYYY-MM-DD HH'
        this.queryParam.format = '%Y-%m-%d %H'
      } else {
        this.format = 'YYYY-MM-DD'
        this.queryParam.format = '%Y-%m-%d'
      }
    },
    loadData(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      if (this.oneLoad === 1) {
        this.oneLoad = 2
        return
      }
      var params = this.getQueryParams()//查询条件
      if (!params.dimension) {
        this.$message.error('请选择查询维度!')
        return
      }
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'line', text: '产线或产线组', dictCode: 'mes_line_cfg,LINE,LINE' })
      fieldList.push({ type: 'string', value: 'eqpno', text: '设备编码', dictCode: '' })
      fieldList.push({ type: 'int', value: 'status', text: '设备状态', dictCode: 'machinestate_status' })
      fieldList.push({ type: 'datetime', value: 'stime', text: '开始时间' })
      fieldList.push({ type: 'datetime', value: 'etime', text: '结束时间' })
      fieldList.push({ type: 'BigDecimal', value: 'dtime', text: '持续时间（秒）', dictCode: '' })
      fieldList.push({ type: 'datetime', value: 'ctime', text: '创建时间' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>